package com.example.azheng.rxjavamvpdemo.ui.acitivity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.ui.fragment.RechargeRecordFragment;
import com.example.azheng.rxjavamvpdemo.ui.fragment.WithdrawRecordFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DepositAndWithdrawalRecordActivity extends AppCompatActivity {


    @BindView(R.id.iv_back_toolbar)
    ImageView ivBackToolbar;
    @BindView(R.id.tv_title_toolbar)
    TextView tvTitleToolbar;
    @BindView(R.id.tvRechargeRecord)
    TextView tvRechargeRecord;
    @BindView(R.id.tvWithdrawalsRecord)
    TextView tvWithdrawalsRecord;
    @BindView(R.id.container)
    FrameLayout container;
    private List<Fragment> mFragmentList;
    private RechargeRecordFragment mRechargeRecordFragment;
    private WithdrawRecordFragment mWithdrawRecordFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit_and_withdrawal_record);
        ButterKnife.bind(this);
        tvTitleToolbar.setText("提现/充值记录");
        mFragmentList = new ArrayList<>();
        mRechargeRecordFragment = new RechargeRecordFragment();
        mWithdrawRecordFragment = new WithdrawRecordFragment();
        showFragment(mRechargeRecordFragment);
    }

    /**
     * 显示fragment
     *
     * @param fragment 要显示的页面
     */
    private void showFragment(Fragment fragment) {

        //不存在列表，中就添加到列表中
        if (!mFragmentList.contains(fragment)) {
            addFragment(fragment);
        }
        //遍历找到要显示得页面
        for (Fragment frag : mFragmentList) {
            if (frag != fragment) {
                getSupportFragmentManager().beginTransaction().hide(frag).commitAllowingStateLoss();
            }
        }
        getSupportFragmentManager().beginTransaction().show(fragment).commitAllowingStateLoss();
    }

    /**
     * 添加fragment
     *
     * @param fragment 要添加的页面
     */
    private void addFragment(Fragment fragment) {

        if (!fragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
            mFragmentList.add(fragment);
        }
    }


    @OnClick({R.id.iv_back_toolbar, R.id.tvRechargeRecord, R.id.tvWithdrawalsRecord})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_toolbar:
                finish();
                break;
            case R.id.tvRechargeRecord:
                showFragment(mRechargeRecordFragment);
                tvRechargeRecord.setTextColor(getResources().getColor(R.color.color_FEFEFF));
                tvWithdrawalsRecord.setTextColor(getResources().getColor(R.color.color_495F7B));
                break;
            case R.id.tvWithdrawalsRecord:
                showFragment(mWithdrawRecordFragment);
                tvRechargeRecord.setTextColor(getResources().getColor(R.color.color_495F7B));
                tvWithdrawalsRecord.setTextColor(getResources().getColor(R.color.color_FEFEFF));
                break;
        }
    }
}